import form
import template
import uix
import util
import weakref
import xtriui
import uiconst
import uicls
import uiutil

def Make_Button(d, **blah):
    ret = uicls.Button(parent=None, label=getattr(mls, d.caption), pos=(d.get('left', 0),
     d.get('top', 0),
     0,
     0))
    ret.autoPos = template.PositionFromStr(d.get('position', 'default'))
    return ret



def Make_TabPanelGroup(d, ids, ignoreMocks = True):
    ret = uicls.Container(name=template.Name(d), align=uiconst.TOALL, pos=(0, 0, 0, 0))
    tabGroup = form.TabGroup(uicls.Container(parent=ret, name='tabs', pos=(0, 0, 0, 0)))
    margin = d.get('margin', 0)
    xmargin = d.get('xmargin', margin)
    ymargin = d.get('ymargin', margin)
    panelParent = uicls.Container(parent=ret, align=uiconst.TOALL, name='panelParent', pos=((xmargin + d.get('innermargin', 0)),
     (ymargin + d.get('innermargin', 0)),
     (xmargin + d.get('innermargin', 0)),
     (ymargin + d.get('innermargin', 0))))
    frame = d.get('frame', 0)
    if frame:
        uicls.Frame(parent=ret, left=(xmargin - 2), top=(ymargin - 2), width=(xmargin - 2), height=(ymargin - 2))
    tabs = []

    def MakeOnTabSelect(panelWr):

        def OnTabSelect(*blah):
            panel = panelWr()
            panel.state = uix.UI_PICKCHILDREN
            for sibling in panel.parent.children:
                if (sibling is not panelWr()):
                    sibling.state = uix.UI_HIDDEN



        return OnTabSelect


    for tabd in map(template.AttrsFromDict, d.tabs):
        panel = uicls.Container(name=tabd.caption, align=uiconst.TOALL, pos=(0, 0, 0, 0))
        panel.OnTabSelect = MakeOnTabSelect(weakref.ref(panel))
        panelParent.children.append(panel)
        template.MakeChildren(panel, tabd.children, ids, ignoreMocks)
        tabs.append((getattr(mls, tabd.caption),
         panel,
         None,
         ((tabd.caption + '_args'))))

    eve.triapp.uilib.desktop.children.append(ret)
    try:
        tabGroup.Startup(tabs)

    finally:
        eve.triapp.uilib.desktop.children.remove(ret)

    return ret



def Make_Combo(d, **blah):
    width = d.get('width', 86)
    left = d.get('left', 0)
    top = d.get('top', 0)
    combo = uicls.Combo(label=_GetCaption(d), options=[], pos=(left,
     top,
     0,
     0), width=width, align=uiconst.RELATIVE, idx=0, adjustWidth=d.get('autoWidth', False))
    return combo



def _GetCaption(d):
    if ('caption' in d):
        return getattr(mls, d.caption)



def Make_Edit(d, **blah):
    minMax = (d.get('minValue', None), d.get('maxValue', None))
    it = d.get('inputType', 'text')
    if (it == 'int'):
        ints = minMax
    else:
        ints = None
    if (it == 'float'):
        floats = minMax
    else:
        floats = None
    ret = uicls.SinglelineEdit(name='', parent=None, left=d.get('left', 0), top=d.get('top', 0), align=template.AlignFromStr(d.get('align', 'none')), ints=ints, floats=floats, label=_GetCaption(d))
    if ('default' in d):
        ret.SetValue(d.default)
    else:
        ret.SetText('', format=False)
    return ret



def Make_Checkbox(d, **blah):
    oldalign = template.AlignFromStr(d.get('align', 'none'))
    newalign = uiutil.GetAlign(oldalign, 0, 0)
    ret = uicls.Checkbox(text=_GetCaption(d), parent=eve.triapp.uilib.desktop, configName='', align=newalign, retval=None, width=d.get('width', 100))
    eve.triapp.uilib.desktop.children.remove(ret)
    return ret


exports = util.AutoExports('template', locals())

